<template>
  <div class="page-main">
    <home-nav :userData="userinfo"></home-nav>
    <div class="container">
      <div class="head-content">
        <span class="bold-font">质量通知单</span>
        <div class="btn-line">
          <!-- <a-button v-if="!quality.id" class="hte-btn add-btn" type="primary" @click="handleSave">保存</a-button> -->
          <a-button v-if="quality.id" class="hte-btn" @click="handleSubmit">去海澜编辑</a-button>
        </div>
      </div>
      <div class="content">
        <section>
          <div class="section-header">基础信息</div>
          <a-form-model ref="ruleForm">
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="质量通知单号:">
                  <a-input v-model="quality.qualityNo" :disabled="true" />
                </a-form-model-item>
              </a-col>
              <a-col :span="16">
                <a-form-model-item
                  label="名称:"
                  prop="name"
                >
                  <a-input v-model="quality.name" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="申请人:" prop="applyUser">
                  <a-input v-model="quality.applyUserName" :disabled="true" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请日期:" prop="applyDate">
                  <a-date-picker valueFormat="YYYY-MM-DD" v-model="quality.applyDate" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="扣罚金额:" prop="fineAmount">
                  <a-input v-model="quality.fineAmount" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="单据类型:" prop="billType">
                  <a-select v-model="quality.billType">
                    <a-select-option
                      v-for="(item, index) in billTypeOptions"
                      :key="'billType' + index"
                      :value="item.value"
                    >
                      {{ item.label }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="涉及合同:" prop="contractName">
                  <a-input disabled v-model="quality.contractName" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="合同编号:" prop="contractNo">
                  <a-input disabled v-model="quality.contractNo" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="供应商:">
                  <a-input disabled v-model="quality.supplierName"> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="PO单号:" prop="poNo">
                  <a-input disabled v-model="quality.poNo" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="WBS号:" prop="wbs">
                  <a-input disabled v-model="quality.wbs" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="违约类型:" prop="qualityTypeArr">
                  <a-select mode="multiple" v-model="quality.qualityTypeArr">
                    <a-select-option
                      v-for="(item, index) in qualityTypeOptions"
                      :key="'billType' + index"
                      :value="item.value"
                    >
                      {{ item.label }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-model-item
                  label="主要内容"
                  prop="content"
                >
                  <a-input v-model="quality.content" type="textarea" :rows="3" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-model-item
                  label="处理要求或建议："
                  prop="suggest"
                >
                  <a-input v-model="quality.suggest" type="textarea" :rows="3" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-model-item
                  label="备注："
                  prop="remark"
                >
                  <a-input v-model="quality.remark" type="textarea" :rows="3" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </section>
        <section v-if="quality.id">
          <div class="section-header">附件信息</div>
          <WorkAttachList busType="quality" :busKey="quality.id + ''" :logBusKey="taskId" />
        </section>
      </div>
    </div>
  </div>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import WorkAttachList from '@/views/workReport/attach/PsWorkAttachList'
export default {
  components: {
    HomeNav: () => import('@/components/layouts/HomeNav'),
    WorkAttachList,
  },
  props: {
    qualityId: {
      type: String,
      default: '-1',
    },
    taskId: {
      type: String,
      default: '-1',
    },
  },
  data() {
    return {
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractReportInfo',
      },
      billTypeOptions: [
        { value: 1, label: '扣罚类(含非扣罚的质量问题)' },
        { value: 2, label: '非扣罚类' },
      ],
      qualityTypeOptions: [
        { value: '1', label: '质量投诉' },
        { value: '2', label: '现场管理投诉' },
        { value: '3', label: '安全事故' },
        { value: '4', label: '进度' },
        { value: '5', label: '延期申请' },
      ],
      urls: {
        qualityInfo: '/hte/contract/qualityInfo',
        qualitySave: '/hte/contract/qualitySave',
        task: '/workReport/task/info',
        project: '/workReport/project/info',
        payment: '/workReport/payment/infoByTask',
      },
      quality: {},
      task: {},
      project: {},
    }
  },
  mounted() {
    if (this.taskId != '-1') {
      this.fetchTask()
    }
    if (this.qualityId != '-1') {
      this.fetchQuality()
    }
  },
  methods: {
    fetchTask() {
      getAction(this.urls.task + '/' + this.taskId, {}).then((res) => {
        if (res.success) {
          this.task = res.result
          if (this.task.projectId) {
            this.fetchProject()
            this.initQuality()
          }
        }
      })
    },
    fetchProject() {
      getAction(this.urls.project + '/' + this.task.projectId, {}).then((res) => {
        if (res.success) {
          this.project = res.result
          if (this.project.id) {
            this.initQuality()
          }
        }
      })
    },
    initQuality() {
      this.$set(this.quality, 'contractNo', this.project.contractNumber)
      this.$set(this.quality, 'contractName', this.project.contractName)
      this.$set(this.quality, 'supplierCode', this.project.supplierCode)
      this.$set(this.quality, 'supplierName', this.project.supplierName)
      this.$set(this.quality, 'companyCode', this.project.signCompanyCode)
      this.$set(this.quality, 'poNo', this.project.busKey)
      this.$set(this.quality, 'wbs', this.task.costNumber)
    },
    fetchQuality() {
      getAction(this.urls.qualityInfo + '/' + this.qualityId, {}).then((res) => {
        if (res.success) {
          this.quality = res.result
          if (!this.quality.id) {
            this.initPayment()
          }
        } else {
          this.initPayment()
        }
      })
    },
    handleSave() {
      this.$set(this.quality, 'qualityType', this.quality.qualityTypeArr.join(','))
      this.$set(this.quality, 'taskId', this.task.id)
      this.$set(this.quality, 'status', 0)
      postAction(this.urls.qualitySave + '/' + this.task.id, this.quality).then((res) => {
        if (res.success) {
          this.quality = res.result
          this.$messageWorkReport.success('保存成功')
          if (this.$route.path.startsWith('/workReport/addQuality')) {
            let urlJump = this.$router.resolve({ path:'/workReport/quality/view/' + this.quality.id + '/' + this.taskId })
            window.open(urlJump.href, '_blank')
            window.close()
          }
        }
      })
    },
    handleSubmit() {
      window.open(process.env.VUE_APP_PS_WEB_BASE_URL+'#editQuality/' + this.quality.id)
    },
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
.page-main {
  .container {
    .content {
      margin-top: 20px;
      section {
        margin-bottom: 18px;
        .section-table {
          .add-btn {
            margin-left: 0;
            margin-bottom: 10px;
          }
          /deep/ .a-upload__input {
            display: none !important;
          }
        }
      }
    }
  }
  .tips-box {
    margin-top: 32px;
    .add-btn {
      width: 180px;
      height: 40px;
      line-height: 40px;
      background-color: #4095e4;
      border-radius: 2px;
    }
  }
}
</style>

